package com.steven.concurrent.volatileTest;

import java.util.concurrent.atomic.AtomicInteger;

public class VolatileTest {
	public static volatile int count;	// 只有保证在单一线程改写值才适用volatile
	public static AtomicInteger actomicCount = new AtomicInteger(0);
	private static final int THREADS_COUNT = 20;
	public static void increase() {
		count++;
		actomicCount.addAndGet(1);
	}
	public static void main(String[] args) {
		Thread[] threads = new Thread[THREADS_COUNT];
		
		for(int i=0; i<THREADS_COUNT; i++) {
			threads[i] = new Thread(new Runnable() {
				@Override
				public void run() {
					for (int i = 0; i < 10000; i++) {
						increase();
					}
				}
			});
			threads[i].start();
		}
		
		// 等待所有累加线程结束
		while (Thread.activeCount() > 1) {
			Thread.yield();
		}
		System.out.println(count);
		System.out.println(actomicCount);
	}
}
